/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxroutingapi                                     */
/* PROCEDURE TYPE        : Routing Load CIM-LOAD Process                    */
/* DESCRIPTION           : Routing Load CIM-LOAD Process                    */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                26-Jul-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}


{xxapilog.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vi_reccount           as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


define temp-table ttrodet no-undo

         field ro_domain    like ro_det.ro_domain   
         field ro_routing   like ro_det.ro_routing  
         field ro_op        like ro_det.ro_op       
         field ro_start     like ro_det.ro_start    
         field ro_std_op    like ro_det.ro_std_op   
         field ro_wkctr     like ro_det.ro_wkctr    
         field ro_mch       like ro_det.ro_mch      
         field ro_desc      like ro_det.ro_desc     
         field ro_Run       like ro_det.ro_Run      
         field ro_end       like ro_det.ro_end      
         field ro_sub_cost  like ro_det.ro_sub_cost 
         field ro_yield_pct like ro_det.ro_yield_pct
         field ro_milestone like ro_det.ro_milestone
         field ro_traceid   as character  
         field ro_dataid    as character  
         field ro_cmdtype   as character 
         field ro_cimfile   as character 
         field ro_logfile   as character 

         index r1 ro_domain ro_routing ro_op
         index r2 ro_traceid
         .
           
   DEFINE DATASET dsRouting FOR ttrodet.

   hdataset = dataset dsRouting:HANDLE.    
                

  
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "ro/"
       m_archpath    = m_archpath + "ro/"
       m_successpath = m_successpath + "ro/"
       m_errorpath   = m_errorpath + "ro/"
       m_workpath    = m_workpath + "ro/".


   empty temp-table ttrodet.
   

   
   v_runproagram = "".
   vc_traceid = "".

   /* Load Bom master data from JSON to temp-table */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
      find first ttrodet no-error.
      assign vc_traceid = ttrodet.ro_traceid.
   end.


    /* validation input data */
   
   /* 校验零件号是否存在 */
   /* 校验开始和结束日期是否重叠 */
   /* 校验工序是否存在 */
   /* */
   /*  */
   /*  */


    /* Create CIM-LOAD file and do cim-load */
   v_cim = string(vc_traceid).
   v_log = string(vc_traceid).
   
  output to /home/mfg/work/cim/xxroutingapi.log  .      
  
 if vc_import and vl_RetOK then do:  

   /* create cim load format */
                 
   cimloadprocess:
   do transaction on error undo cimloadprocess, leave cimloadprocess: 
   
   for each ttrodet  break by ttrodet.ro_routing.

    if first-of(ttrodet.ro_routing) then do:

      
     v_cim = m_workpath + "RO" + "_" + string(ttrodet.ro_traceid) + "_" + string(ttrodet.ro_routing) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
     v_log = m_workpath + "RO" + "_" + string(ttrodet.ro_traceid) + "_" + string(ttrodet.ro_routing) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".

     output stream f_cim to value(v_cim). 

     end.
     find last ro_det where ro_det.ro_domain    = global_domain
                        and ro_det.ro_routing   = ttrodet.ro_routing
                        and ro_det.ro_op        = ttrodet.ro_op
                        and ro_det.ro_start     = ttrodet.ro_start                                        
                        no-lock no-error.
                        
         if available ro_det then do:
         /* update routing */
         put stream f_cim unformatted
                                         v_quote ttrodet.ro_routing       v_quote  v_space                      /* Routing */
                                         v_quote ttrodet.ro_op            v_quote  v_space                      /* OPERATION */
                                         ttrodet.ro_start. 
         if ttrodet.ro_cmdtype = "D" then put stream f_cim unformatted v_space "X" skip.
         else put stream f_cim unformatted skip.                          /* Start Date*/    
         put stream f_cim unformatted
                                         v_quote ttrodet.ro_wkctr v_quote v_space                               /* Work CENTER */
                                         v_default v_space  skip                                                /* Machine */
                                         v_quote ttrodet.ro_desc v_quote v_space                                /* DESCRIPTION */
                                         v_default v_space                                                      /* Machine Quantity */
                                         v_default v_space                                                      /* TRANS PER */
                                         v_default v_space                                                      /* Queue */
                                         v_default v_space                                                      /* Wait Time */
                                         v_quote ttrodet.ro_milestone v_quote v_space                           /* Milestone */
                                         v_default v_space                                                      /* Subcontract LT */
                                         v_default v_space                                                      /* Setup Screw */
                                         v_default v_space                                                      /* Run Screw */
                                         v_default v_space                                                      /* Setup Time */
                                         v_quote ttrodet.ro_Run v_quote  v_space                                /* Run Time */
                                         v_default v_space                                                      /* Move Time */
                                         ttrodet.ro_start v_space                                               /* Start Date */
                                         ttrodet.ro_end v_space                                                 /* End Date */
                                         v_default v_space                                                      /* Percent */
                                         v_default v_space                                                      /* Tooling Code */
                                         v_default v_space                                                      /* Supplier */
                                         v_default v_space                                                      /* Inventory Value */
                                         ttrodet.ro_sub_cost v_space                                             /* Subcontract Cost */
                                         v_quote "N" v_quote                                                    /* Comment */
                                         skip. 
                                         if ttrodet.ro_cmdtype = "D" then put stream f_cim unformatted "YES" skip.    
                                         else
                                         put stream f_cim unformatted                                                                                 
                                         v_default v_space                                                      /* WIP　Part */
                                         v_quote "Y" v_quote v_space                                            /* Move to next operation */
                                         v_quote "y" v_quote v_space                                            /* Auto labor report */
                                         skip.         

    end. /* if available ro_det */                                
         else do:
                   put stream f_cim unformatted
                          v_quote ttrodet.ro_routing       v_quote  v_space                      /* Routing */
                          v_quote ttrodet.ro_op            v_quote  v_space                      /* OPERATION */
                          v_quote ttrodet.ro_start v_quote skip                                  /* Start Date */
                          v_default       skip                                                   /* stardard operation */
                          v_quote ttrodet.ro_wkctr v_quote v_space                               /* Work CENTER */
                          v_default v_space  skip                                                /* Machine */
                          v_quote ttrodet.ro_desc v_quote v_space                                /* DESCRIPTION */
                          v_default v_space                                                      /* Machine Quantity */
                          v_default v_space                                                      /* TRANS PER */
                          v_default v_space                                                      /* Queue */
                          v_default v_space                                                      /* Wait Time */
                          v_quote ttrodet.ro_milestone v_quote v_space                           /* Milestone */
                          v_default v_space                                                      /* Subcontract LT */
                          v_default v_space                                                      /* Setup Screw */
                          v_default v_space                                                      /* Run Screw */
                          v_default v_space                                                      /* Setup Time */
                          v_quote ttrodet.ro_Run  v_quote v_space                                /* Run Time */
                          v_default v_space                                                      /* Move Time */
                          v_default v_space                                                      /* Start Date */
                          ttrodet.ro_end  v_space                                                /* End Date */
                          v_default v_space                                                      /* Percent */
                          v_default v_space                                                      /* Tooling Code */
                          v_default v_space                                                      /* Supplier */
                          v_default v_space                                                      /* Inventory Value */
                          ttrodet.ro_sub_cost v_space                                            /* Subcontract Cost */
                          v_quote "N" v_quote                                                    /* Comment */
                          skip     
                          v_default v_space                                                      /* WIP　Part */
                          v_quote "Y" v_quote v_space                                            /* Move to next operation */
                          v_quote "y" v_quote v_space                                            /* Auto labor report */
                          skip.         

        end. 
     
   
    if last-of(ttrodet.ro_routing) then do:
    output stream f_cim close.
    
   
         
     /* cim load process */
     v_runproagram = "rwromt.p".
     execname = "xxrodetapi.p".

     
       put unformatted v_cim " " skip
                       v_log " " skip 
                       m_outpath " " skip
                       m_archpath " " skip
                       v_runproagram " " skip
                       v_errstring " " skip 
                       v_errorstatus skip .
                       
       run cimloadprocess(input v_cim,
                          input v_log,
                          input v_runproagram,
                          input vc_traceid,
                          input ttrodet.ro_dataid,
                          input ttrodet.ro_routing,
                          output v_errstring,
                          output v_errorstatus) .
                          
       v_toterrorstatus = v_toterrorstatus or v_errorstatus.    


            /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                 
       end. /* do vi_filecount = 1 to 2: */  

          assign   ttrodet.ro_cimfile = replace(v_cim,m_workpath,vc_topath)
                   ttrodet.ro_logfile = replace(v_log,m_workpath,vc_topath).


              
     end. /* if last-of */
     
    end. /* for each ttrodet  */
     if  v_toterrorstatus then 
                 undo cimloadprocess, leave cimloadprocess.          
            
        
    end.    /* do transaction on error undo cimloadprocess, leave cimloadprocess: */
       if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
       end.  /* if can-find(first api_status */
         


   end. /* if vc_import */
   
      /* Create LOG record */
   
     for each ttrodet break by ttrodet.ro_routing .
       if last-of(ttrodet.ro_routing) then 
       do:
       vi_reccount = vi_reccount + 1.
              
       run CreateAPILogMaster(input vc_traceid,
                              input "createroutingdet",
                              input "xxrodetapi.p",
                              input ttrodet.ro_cimfile,
                              input ttrodet.ro_logfile,
                              input vc_inputjson,
                              input ttrodet.ro_routing,
                              input "",
                              input "xxapiitemlocdata_det",
                              input vi_reccount).  
                              
       end. /*  if last-of(ttrodet.pti_part) then  */
        
      end. /*   for each ttrodet break by ttrodet.ro_routing . */
   
   
   
   
   
   
   
        /* Handle cimload related files */
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
         put unformatted v_cim " " skip
                         v_log " " skip 
                         m_outpath " " skip
                         m_archpath " " skip
                         v_runproagram " " skip
                         v_errstring " " skip 
                         v_errorstatus skip .
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
       
       output close.   
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
